// api/userApi.js
const request = (url, method, data) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: url,
      method: method,
      data: data,
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync('token') || ''
      },
      success: (res) => {
        if (res.statusCode === 200) {
          resolve(res.data);
        } else {
          reject(res);
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};

// 基础URL，实际开发中需要替换为真实的API地址
const BASE_URL = 'https://api.example.com';

const userApi = {
  // 用户登录
  login: (code) => {
    return request(
      `${BASE_URL}/api/user/login`, 
      'POST', 
      { code }
    );
  },
  
  // 获取用户信息
  getUserInfo: () => {
    return request(
      `${BASE_URL}/api/user/info`, 
      'GET'
    );
  },
  
  // 获取用户创作的图片列表
  getUserImages: (page = 1, limit = 10) => {
    return request(
      `${BASE_URL}/api/user/images`, 
      'GET', 
      { page, limit }
    );
  },
  
  // 收藏/取消收藏图片
  toggleFavorite: (imageId) => {
    return request(
      `${BASE_URL}/api/user/favorite`, 
      'POST', 
      { imageId }
    );
  }
};

module.exports = userApi; 